package cn.edu.sdjzu.xg.bysj.dao;

import cn.edu.sdjzu.xg.bysj.domain.ApplicationForSupervisorEntry;
import cn.edu.sdjzu.xg.bysj.domain.authority.*;
import cn.edu.sdjzu.xg.bysj.service.ResourceService;
import cn.edu.sdjzu.xg.bysj.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

@Slf4j
public class RoleAssResourceDao {
    //创建RoleAssResourceDao对象
    private static RoleAssResourceDao roleAssResourceDao = new RoleAssResourceDao();
    //将唯一构造器私有，阻止本类外生成本类的对象
    private RoleAssResourceDao(){}
    //获取实例
    public static RoleAssResourceDao getInstance(){
        return roleAssResourceDao;
    }

    /**
     * 增加
     * @param connection
     * @param role_id
     * @param  resources
     * @return
     * @throws SQLException
     */
    public boolean add(Connection connection, int role_id , Collection<Resource> resources) throws SQLException {
        //添加进表中
        for (Resource resource : resources){
            log.trace(resource.toString());
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO RoleAssResource " +
                    "(role_id,resource_id)VALUES (?,?)");
            preparedStatement.setInt(1,role_id);
            preparedStatement.setInt(2,resource.getId());
            int RowAffected = preparedStatement.executeUpdate();
            if (RowAffected <= 0 ){
                return false;
            }
            JdbcHelper.close(null,preparedStatement);
        }
        return true;
    }
    /**
     * 查找
     * @param connection
     * @param roleId
     * @return
     * @throws SQLException
     */
    public Collection<RoleAssResource> findAll(Connection connection, int roleId) throws SQLException {
        //创建集合类对象，用来保存所有记录代表的ActorAssRole对象
        //TreeSet按compare(Object o)方法指定的顺序排序
        Collection<RoleAssResource> roleAssResources = new TreeSet<RoleAssResource>();
        //准备预编译的语句
        String sql_select_all = "SELECT * FROM RoleAssResource WHERE role_id = ?";
        PreparedStatement pstmt_select_all =
                connection.prepareStatement(sql_select_all);
        pstmt_select_all.setInt(1, roleId);
        //执行预编译语句，结果集保存在resultSet中
        ResultSet resultSet_select_all = pstmt_select_all.executeQuery();
        //遍历resultSet
        while (resultSet_select_all.next()) {
            int id = resultSet_select_all.getInt("id");
            //获得actorAssRole表中当前记录的id字段的值
            int role_id = resultSet_select_all.getInt("role_id");
            int resourceId = resultSet_select_all.getInt("resource_id");
            //获得当前ActorAssRole关联的Role
            Resource resource = ResourceService.getInstance().find(resourceId);
            RoleAssResource roleAssResource =
                    new RoleAssResource(id, null, resource);
            //将当前记录代表的ActorAssRole对象添加到desiredActorAssRoles中
            roleAssResources.add(roleAssResource);
        }
        JdbcHelper.close(resultSet_select_all, pstmt_select_all);
        return roleAssResources;
    }
}
